package cn.iocoder.yudao.module.vos.controller.app.marketingscore.vo;

import java.util.Map;

public class MarketingScoreApiSqlProvider {

    /**
     * 取当日表的 Top-N（按 score DESC、create_time DESC、phone_e164 DESC 排序），
     * 然后在该集合里分页，只返回 phone_md5。
     */
    public static String buildSelectTopPhoneMd5Page(Map<String, Object> params) {
        StringBuilder sb = new StringBuilder(512);
        sb.append("<script>");
        sb.append("SELECT t.phone_md5 FROM (");
        sb.append("  SELECT phone_md5, score, create_time, phone_e164");
        sb.append("  FROM ${table}");
        sb.append("  ORDER BY score DESC, create_time DESC, phone_e164 DESC");
        sb.append("  LIMIT #{quota}");
        sb.append(") t ");
        sb.append("ORDER BY t.score DESC, t.create_time DESC, t.phone_e164 DESC ");
        sb.append("LIMIT #{limit} OFFSET #{offset}");
        sb.append("</script>");
        return sb.toString();
    }

    /** Top-N 的总数（= min(quota, 实际条数)） */
    public static String buildCountTop(Map<String, Object> params) {
        StringBuilder sb = new StringBuilder(256);
        sb.append("<script>");
        sb.append("SELECT COUNT(1) FROM (");
        sb.append("  SELECT 1 FROM ${table} ");
        sb.append("  ORDER BY score DESC, create_time DESC, phone_e164 DESC ");
        sb.append("  LIMIT #{quota}");
        sb.append(") t");
        sb.append("</script>");
        return sb.toString();
    }
}
